package br.com.controlador;

import br.com.modelo.PreVendaCabecalho;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.FlowEvent;

/**
 *
 * @author gilberto.costa
 */
@ManagedBean
@ViewScoped
public class PreVendaCabecalhoControler implements Serializable {

    private PreVendaCabecalho preVendaCabecalho;
    private String campoBusca;
    private List<PreVendaCabecalho> listPreVendaCabecalho = new ArrayList<PreVendaCabecalho>();

    
    private boolean skip;  
    private static final Logger logger = Logger.getLogger(PreVendaCabecalhoControler.class.getName());
    
    @PostConstruct
    public void init() {
        listPreVendaCabecalho = PreVendaCabecalho.retornaPreVendaCabecalho();
    }

    public void inicializaObj() {
        preVendaCabecalho = new PreVendaCabecalho();
    }

    public void salvar() {
        try {
            preVendaCabecalho = preVendaCabecalho.salvar();
            listPreVendaCabecalho = PreVendaCabecalho.retornaPreVendaCabecalho();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Pré venda salva!!", "Mensagem"));
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Problema ao cadastrar a pré venda!!" + e, "Mensagem"));
        }
    }

    public void excluir() {
        try {
            preVendaCabecalho.excluir();
            listPreVendaCabecalho = PreVendaCabecalho.retornaPreVendaCabecalho();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Aliquota excluido!!", "Mensagem"));
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Erro ao excluir aliquota!!" + e, "Mensagem"));
        }
    }

    public void realizaBusca() {
        listPreVendaCabecalho = PreVendaCabecalho.retornaBusca(campoBusca);
    }

    public void inicializaBusca() {
        campoBusca = new String();
    }
    
    public String onFlowProcess(FlowEvent event) {  
        logger.info("Current wizard step:" + event.getOldStep());  
        logger.info("Next step:" + event.getNewStep());  
          
        if(skip) {  
            skip = false;   //reset in case user goes back  
            return "confirm";  
        }  
        else {  
            return event.getNewStep();  
        }  
    }  
}